package impl;

import com.zhao.bike.*;
import consts.BikeConstans;
import dao.BikeDao;
import util.DateUtil;

import java.util.Arrays;
import java.util.Scanner;

/**
 * @classname:BikeDaoImpl
 * @description:
 * @author: Mr.Z
 * @date: 2020/12/3 22:46
 * @version: 0.1
 * @since:1.8
 */
public class BikeDaoImpl implements BikeDao {

    @Override
    public void selectBike(SharedBikeCompany[] companies) {
        for (SharedBikeCompany company : companies) {
            System.out.println("+----------+---------+--------------------+----------------+");
            System.out.println("| 公司序号 | 公司名称 |   公司单车数量      |  公司借出次数   |");
            System.out.println("|   " + company.getBikeCompanyId() + "\t|" + company.getBikeCompanyName() + "\t|\t\t\t" +
                    "" + company.getSum() + "\t\t|\t\t" + company.getCount());
            System.out.println("+----------+---------+--------------------+----------------+");
            System.out.println();
            showCompanyBikes(company);
            System.out.println("+----------+---------+--------------------+----------------+");
        }
    }

    @Override
    public void addBike(int addNum, SharedBikeCompany company) {
        SharedBike[] bikes = company.getSharedBikes();
        int sum = company.getSum();
        //copyOf已经创建新的数组所以要重新赋值给该公司的单车
        bikes = Arrays.copyOf(bikes, (sum + addNum));
        System.out.println("空间不足已经按照配置重新扩容");
        int id = company.getBikeCompanyId();
        int bid = sum + 1;
        for (int i = 0; i < addNum; i++) {
            switch (id) {
                case 1:
                    bikes[sum] = new OfoBike(bid, BikeConstans.OFO_BIKE_NAME + bid, 1, null);
                    break;
                case 2:
                    bikes[sum] = new MobaiBike(bid, BikeConstans.MOBAI_COMPANY_NAME + bid, 1, null);
                    break;
                case 3:
                    bikes[sum] = new HaloBike(bid, BikeConstans.HALO_COMPANY_NAME + bid, 1, null);
                    break;
            }
            sum++;
            bid++;
        }
        company.setSharedBikes(bikes);
        company.setSum(company.getSum() + addNum);
        System.out.println("投放" + addNum + "辆" + company.getBikeCompanyName() + "成功");
        System.out.println("-----------------------");
    }

    @Override
    public void deleteBike(SharedBikeCompany company,Scanner input) {
        System.out.println(company.getBikeCompanyName() + "的共享单车如下");
        showCompanyBikes(company);
        System.out.println("请录入要删除单车的id");
        int id = input.nextInt();
        int sum = company.getSum();
        SharedBike[] bikes = company.getSharedBikes();
        boolean flag = false;
        for (int i = 0; i < sum; i++) {
           SharedBike bike = bikes[i];
           if(id == bike.getBid()){
               flag = true;
               if(bike.getStatus()==0){
                   System.out.println("该单车正在被接触无法删除");
                   return;
               }
               for(int j = i ;j<sum-1;j++){
                   bikes[j] = bikes[j+1];
               }
               bikes[sum-1] = null;
               break;
           }
        }
        if(!flag){
            System.out.println("没有查询到该Id单车");
            return;
        }
        System.out.println("删除"+id+"成功");
        company.setSharedBikes(bikes);
        company.setSum(company.getSum()-1);

    }




    public void showCompanyBikes(SharedBikeCompany company) {
        System.out.println("单车序号   单车品牌    单车状态    借出时间");
        for (int i1 = 0, len1 = company.getSum(); i1 < len1; i1++) {
            //判断自行车是否为空
            if (company.getSharedBikes()[i1] == null) {
                break;
            }
            SharedBike sharedBike = company.getSharedBikes()[i1];
            String status = (sharedBike.getStatus() == 1) ? "可借" : "已借出";
            String borrowTime = (sharedBike.getBorrowTime() == null) ? "\t\t_" : sharedBike.getBorrowTime();
            System.out.println("\t" + company.getSharedBikes()[i1].getBid() + "\t|" +
                    " " + company.getSharedBikes()[i1].getBname() + "\t|\t" + status + "\t\t|" + borrowTime);
        }
    }


    @Override
    public void borrowBike(SharedBikeCompany company, Scanner input) {
        System.out.println("此"+company.getBikeCompanyName()+"共有共享单车如下");
        showCompanyBikes(company);
        System.out.println("请录入要借出单车编号");
        int id = input.nextInt();

        int sum = company.getSum();
        SharedBike[] bikes = company.getSharedBikes();
        boolean flag = false;
        for (int i = 0; i < sum; i++) {
           SharedBike bike = bikes[i];
           if(id==bike.getBid()){
               flag = true;
               if(bike.getStatus()==0){
                   System.out.println("该单车已经被借出请查证后在输入");
                   return;
               }
               System.out.println("请输入借出时间");
               input.nextLine();
               //整行取值，所以用nextLine
               String boorowTime = input.nextLine();
               bike.setBorrowTime(boorowTime);
               bike.setStatus(0);
               System.out.println("借出"+company.getBikeCompanyName()+"下的<<"+bike.getBname()+">>成功");
               company.setCount(company.getCount()+1);
               break;
           }
        }

        if(!flag){
            System.out.println("没有该单车Id请查证后再输入");
           return;
        }

    }



    @Override
    public void returnBike(SharedBikeCompany company, Scanner input) {
        System.out.println("请输入要归还的单车编号");
        int id = input.nextInt();
        int sum = company.getSum();
        SharedBike[] bikes = company.getSharedBikes();
        boolean flag  = false;
        for (int i = 0; i < sum; i++) {
            SharedBike bike = bikes[i];
            if(id==bike.getBid()){
                flag = true;
                if(bike.getStatus()==1){
                    System.out.println("单车正库无需归还");
                    return;
                }

                System.out.println("请输入归还时间");
                input.nextLine();
                String returnTime = input.nextLine();

                System.out.println("您的借车时间为"+bike.getBorrowTime());
                int duringTime = DateUtil.getDuringTime(bike.getBorrowTime(), returnTime, BikeConstans.PATTERN);
                System.out.println("用车时间为<<"+duringTime+"小时>>,需要支付:"+duringTime+"元");
                bike.setStatus(1);
                bike.setBorrowTime(null);
                break;
            }
        }
        if(!flag){
            System.out.println("没有找到要归还的单车");
            return;
        }
        System.out.println("归还id为"+id+"成功");

    }

    @Override
    public void rankBike(SharedBikeCompany[] sharedBikeCompanies) {

        SharedBikeCompany[] companies =sharedBikeCompanies;

        int comlen = companies.length;
        for (int i = 0; i < comlen; i++) {
            for (int j = 0;j<comlen-i-1;j++){
                if(companies[j].getCount()<companies[j+1].getCount()){
                    SharedBikeCompany temp = companies[j+1];
                    companies[j+1] = companies[j];
                    companies[j] = temp;
                }
            }
        }
        System.out.println("公司名称\t\t"+"借车次数");
        for (int i = 0; i < comlen; i++) {
            System.out.println(companies[i].getBikeCompanyName()+"\t\t"+companies[i].getCount());
        }
    }

}
